import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getSimpleDeviceGroupList } from '#/api/iot/device/group';
import {
  DeviceTypeEnum,
  getSimpleProductList,
} from '#/api/iot/product/product';

import { $t } from '#/locales';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'productId',
      label: $t('views.iot.device.device.productId'),
      component: 'ApiSelect',
      componentProps: {
        api: getSimpleProductList,
        labelField: 'name',
        valueField: 'id',
        placeholder: $t('views.iot.device.device.productIdPlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'deviceName',
      label: $t('views.iot.device.device.deviceName'),
      component: 'Input',
      componentProps: {
        placeholder: '请输入 DeviceName',
      },
      rules: z
        .string()
        .min(4, $t('views.iot.device.device.deviceNameMinLength'))
        .max(32, $t('views.iot.device.device.deviceNameMaxLength'))
        .regex(/^[\w.\-:@]{4,32}$/, $t('views.iot.device.device.deviceNamePattern')),
    },
    {
      fieldName: 'gatewayId',
      label: $t('views.iot.device.device.gatewayId'),
      component: 'ApiSelect',
      componentProps: {
        api: async () => {
          const { getSimpleDeviceList } = await import(
            '#/api/iot/device/device'
          );
          return getSimpleDeviceList(DeviceTypeEnum.GATEWAY);
        },
        labelField: 'nickname',
        valueField: 'id',
        placeholder: $t('views.iot.device.device.gatewayIdPlaceholder'),
      },
      dependencies: {
        triggerFields: ['deviceType'],
        show: (values) => values.deviceType === 1, // GATEWAY_SUB
      },
    },
    {
      fieldName: 'nickname',
      label: $t('views.iot.device.device.nickname'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.iot.device.device.nicknamePlaceholder'),
      },
      rules: z
        .string()
        .min(4, $t('views.iot.device.device.nicknameMinLength'))
        .max(64, $t('views.iot.device.device.nicknameMaxLength'))
        .regex(
          /^[\u4E00-\u9FA5\u3040-\u30FF\w]+$/,
          $t('views.iot.device.device.nicknamePattern')
        )
        .optional()
        .or(z.literal('')),
    },
    {
      fieldName: 'groupIds',
      label: $t('views.iot.device.device.group'),
      component: 'ApiSelect',
      componentProps: {
        api: getSimpleDeviceGroupList,
        labelField: 'name',
        valueField: 'id',
        mode: 'multiple',
        placeholder: $t('views.iot.device.device.groupPlaceholder'),
      },
    },
    {
      fieldName: 'serialNumber',
      label: $t('views.iot.device.device.serialNumber'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.iot.device.device.serialNumberPlaceholder'),
      },
      rules: z
        .string()
        .regex(/^[\w-]+$/, $t('views.iot.device.device.serialNumberPattern'))
        .optional()
        .or(z.literal('')),
    },
    // {
    //   fieldName: 'locationType',
    //   label: '定位类型',
    //   component: 'RadioGroup',
    //   componentProps: {
    //     options: getDictOptions(DICT_TYPE.IOT_LOCATION_TYPE, 'number'),
    //     buttonStyle: 'solid',
    //     optionType: 'button',
    //   },
    // },
    {
      fieldName: 'longitude',
      label: $t('views.iot.device.device.longitude'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.iot.device.device.longitudePlaceholder'),
        class: 'w-full',
      },
      dependencies: {
        triggerFields: ['locationType'],
        show: (values) => values.locationType === 3, // MANUAL
      },
    },
    {
      fieldName: 'latitude',
      label: $t('views.iot.device.device.latitude'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.iot.device.device.latitudePlaceholder'),
        class: 'w-full',
      },
      dependencies: {
        triggerFields: ['locationType'],
        show: (values) => values.locationType === 3, // MANUAL
      },
    },
  ];
}

/** 设备分组表单 */
export function useGroupFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'groupIds',
      label: $t('views.iot.device.device.group'),
      component: 'ApiSelect',
      componentProps: {
        api: getSimpleDeviceGroupList,
        labelField: 'name',
        valueField: 'id',
        mode: 'multiple',
        placeholder: $t('views.iot.device.device.groupPlaceholder'),
      },
      rules: 'required',
    },
  ];
}

/** 设备导入表单 */
export function useImportFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'file',
      label: $t('views.iot.device.device.importFile'),
      component: 'Upload',
      rules: 'required',
      help: $t('views.iot.device.device.importFileHelpMessage'),
    },
    {
      fieldName: 'updateSupport',
      label: $t('views.iot.device.device.updateSupport'),
      component: 'Switch',
      componentProps: {
        checkedChildren: '是',
        unCheckedChildren: '否',
      },
      rules: z.boolean().default(false),
      help: $t('views.iot.device.device.updateSupportHelpMessage'),
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'productId',
      label: $t('views.iot.device.device.productId'),
      component: 'ApiSelect',
      componentProps: {
        api: getSimpleProductList,
        labelField: 'name',
        valueField: 'id',
        placeholder: $t('views.iot.device.device.productIdPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'deviceName',
      label: $t('views.iot.device.device.deviceName'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.iot.device.device.deviceNamePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'nickname',
      label: $t('views.iot.device.device.nickname'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.iot.device.device.nicknamePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'deviceType',
      label: $t('views.iot.device.device.deviceType'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.IOT_PRODUCT_DEVICE_TYPE, 'number'),
        placeholder: $t('views.iot.device.device.deviceTypePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'status',
      label: $t('views.iot.device.device.status'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.IOT_DEVICE_STATUS, 'number'),
        placeholder: $t('views.iot.device.device.statusPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'groupId',
      label: $t('views.iot.device.device.group'),
      component: 'ApiSelect',
      componentProps: {
        api: getSimpleDeviceGroupList,
        labelField: 'name',
        valueField: 'id',
        placeholder: $t('views.iot.device.device.groupPlaceholder'),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'deviceName',
      title: $t('views.iot.device.device.deviceName'),
      minWidth: 150,
    },
    {
      field: 'nickname',
      title: $t('views.iot.device.device.nickname'),
      minWidth: 120,
    },
    {
      field: 'productId',
      title: $t('views.iot.device.device.productId'),
      minWidth: 120,
      slots: { default: 'product' },
    },
    {
      field: 'deviceType',
      title: $t('views.iot.device.device.deviceType'),
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.IOT_PRODUCT_DEVICE_TYPE },
      },
    },
    {
      field: 'groupIds',
      title: $t('views.iot.device.device.group'),
      minWidth: 150,
      slots: { default: 'groups' },
    },
    {
      field: 'status',
      title: $t('views.iot.device.device.status'),
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.IOT_DEVICE_STATUS },
      },
    },
    {
      field: 'onlineTime',
      title: $t('views.iot.device.device.onlineTime'),
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      title: $t('common.action'),
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
